<?php
/**
 *author:韩帅
 *createtime:2017/6/20 9:51
 *description:
 */
namespace App\Http\Controllers\App;

use App\Models\CardType;
use App\Models\Banner;
use Illuminate\Http\Request;

class CardTypeController extends BaseController
{
    /**
     * 银行类型列表
     */
    public function cardTypeList(Request $request)
    {
        $return = [];
        $name   = trim($request->input('name',''));

        $obj  = new CardType();

        if (!empty($name)) {

            $obj = $obj->where('name','like','%'.$name.'%');
        }

        $data = $obj->paginate($this->pageSize)->toArray();

        $return = $data;

        if (empty($return['data'])) {
            return self::json($return,'数据为空',200);
        }
        return self::json($data,'成功',200);
    }

    /**
     * 银行卡类型添加
     */
    public function cardTypeAdd(Request $request)
    {

        $name = trim($request->input('name',''));
        $logo = trim($request->input('logo',''));

        if (empty($name) || empty($logo)) {
            return self::error('必填字段不能为空');
        }

        //验重
        $res = $this->cardTypeCheck($name);
        if ($res) {
            return self::error('不能重复添加');
        }

        $obj = new CardType();

        $obj->name = $name;
        $obj->logo = $logo;

        $res = $obj->save();

        if ($res == false) {
            return self::error('添加失败');
        }
        return self::success('成功');
    }

    /**
     * 银行信息的修改
     */
    public function cardTypeUpdate(Request $request)
    {

        $id    = $request->input('id','');
        $name  = trim($request->input('name',''));
        $logo  = trim($request->input('logo',''));

        if (empty($name) || empty($logo)) {
            return self::error('必填字段不能为空');
        }

        $obj   = CardType::find($id);

        if (empty($obj)) {
            return self::error('记录不存在');
        }

        $obj->name = $name;
        $obj->logo = $logo;

        $res = $obj->save();

        if ($res == false) {
            return self::error('更新失败');
        }
        return self::success('成功');
    }

    /**
     * 银行类型的启用禁用
     */
    public function cardTypeStatus(Request $request)
    {

        $id     = $request->input('id','');
        $status = intval($request->input('status',''));

        if (empty($id)) {

            return self::error('必填字段不能为空');
        }

        $obj   = CardType::find($id);

        if (empty($obj)) {
            return self::error('记录不存在');
        }

        $obj->status = $status;

        $res = $obj->save();

        if ($res == false) {
            return self::error('更新失败');
        }
        return self::success('成功');
    }

    /**
     * 银行卡类型添加验证
     */
    public function cardTypeCheck($name) {

        $info = CardType::where('name',$name)->first();
        if (!empty($info)) {
            return true;
        }
        return false;
    }
}